package com.bite.LoopQueue.Flg;

public class Demo {

    //cur在前，dest在后
    int cur;
    int dest;
    //表示队列为空
    boolean flg = false;
    //用数组创建队列
    int[] Myqueue;

    public Demo(int k) {
        Myqueue = new int[k];
    }

    public boolean enQueue(int value) {
        if (isFull()) {
            return false;
        }
        Myqueue[cur] = value;
        //向后移动一位
        cur = (cur + 1) % Myqueue.length;
        flg = true;
        return true;
    }

    public boolean deQueue() {
        if (isEmpty()) {
            return false;
        }
        dest = (dest + 1) % Myqueue.length;
        flg = false;
        return true;
    }

    //队列从队列头开始进
    //队列首
    public int Front() {
        if (isEmpty()) {
            return -1;
        }
        return Myqueue[dest];
    }

    //队列尾
    public int Rear() {
        if (isEmpty()) {
        return -1;
    }
        int ret = cur == 0 ? Myqueue.length - 1 : cur - 1;
        return Myqueue[ret];
    }

    public boolean isEmpty() {
        if (cur == dest && flg == false) {
            return true;
        }
        return false;
    }

    public boolean isFull() {
        if (cur == dest && flg == true) {
            return true;
        }
        return false;
    }
}
